import { AppNotify, NotifyOption, NotifyType } from "./app.notify";
import { LoginService } from "./services/login.service";

// app.ts
App<IAppOption>({
  globalData: {},
  onLaunch() {
    console.log('app.ts onLaunch >>>>');

    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        console.log(res);
        LoginService.instance.loginWX(res.code).then(() => {
          AppNotify.instance.send(NotifyType.Login, NotifyOption.Empty, '');
        });
        // 发送 res.code 到后台换取 openId, sessionKey, unionId

      },
    })

    /**动态加载网络字体 */
    wx.loadFontFace({
      // global: true,
      family: 'OPPOSANS',
      source: 'url("http://api.lxyy.fun/font/OPPOSANS-H.TTF")',
      success: function (res) {
        console.log(res) //  loaded
      },
      fail: function (res) {
        console.log(res) //  error
      }
    })

    /**监听网络状态变化事件 */
    wx.onNetworkStatusChange(function (res) {
      if (res.isConnected) {
        if (res.networkType === "wifi") {
          wx.showToast({ title: '当前已切换至WiFi网络', icon: 'none' })
        } else {
          wx.showToast({ title: '当前已切换至非WiFi网络，请注意流量消耗', icon: 'none' })
        }
      } else {
        wx.showToast({ title: '网络无连接,请检查网络连接', icon: 'none' })
      }
    })
  },
})